//
//  TitleButton.m
//  News
//
//  Created by 徐涵菲 on 2018/4/23.
//  Copyright © 2018年 Wangly. All rights reserved.
//

#import "TitleButton.h"

@implementation TitleButton

// 通过代码创建控件时会调用
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

// 通过xib/Storboard创建时调用
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setup];
    }
    return self;
}

-(void)setup
{
    //进行一次性的初始化
    //设置颜色
    [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //设置图片居中
    self.imageView.contentMode=UIViewContentModeCenter;
    //设置标题居中
    self.titleLabel.textAlignment=NSTextAlignmentCenter;
}

/*
 
 -(CGRect)titleRectForContentRect:(CGRect)contentRect
 {
 CGFloat titleX =0;
 CGFloat titleY =0;
 CGFloat titleW =contentRect.size.width - 30;
 CGFloat titleH =contentRect.size.height;
 return CGRectMake(titleX, titleY, titleW, titleH);
 }
 
 -(CGRect)imageRectForContentRect:(CGRect)contentRect
 {
 CGFloat imageX =contentRect.size.width - 30;
 CGFloat imageY =0;
 CGFloat imageW =30;
 CGFloat imageH =contentRect.size.height;
 return CGRectMake(imageX, imageY, imageW, imageH);
 }
 */

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.titleLabel.x=self.imageView.x;
    
    self.imageView.x=CGRectGetMaxX(self.titleLabel.frame) + 5;
}
@end
